import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { SalesmanService } from '@core/routes-services/salesman.service';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { DistrictService } from '@core/routes-services/district.service';
import { SalesmanTypeService } from '@core/routes-services/salesman-type.service';

@Component({
  selector: 'app-salesman-detail',
  templateUrl: './salesman-detail.component.html',
})
export class SalesmanDetailComponent implements OnInit {
  id: any = 0;
  entity: any = {};
  isLoading: boolean = true;

  role:any = 1;//角色: 1-业务员，2-渠道商，3-门店，4-代理

  districtOption:any [] = [];
  salesManTypeOption:any [] = [];
  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private message: NzMessageService,
    private salesmanService: SalesmanService,
    private districtService:DistrictService,
    private salesmanTypeService:SalesmanTypeService,
  ) {

  }


  ngOnInit(): void {
    this.initDistrice()
    this.route.queryParams.forEach((params: Params) => {
      this.id = +params['id'];
      this.role = +params['role'];
      this.initEntity();
    });
  }

  initEntity() {
    this.isLoading = true;
    this.salesmanService.detail(this.id).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.entity = res.data;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  initDistrice(){
    this.districtService.getAll().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.districtOption = res.data;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error)
    });
    this.salesmanTypeService.getAll().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.salesManTypeOption = res.data.marketSalesmanTypeList;
        if(this.role == 3){
          this.salesManTypeOption = this.salesManTypeOption.filter((d)=>d.type == 1);
        }else {
          this.salesManTypeOption = this.salesManTypeOption.filter((d)=>d.type == 2);
        }
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error)
    });
  }

  close(e) {
    console.log(e);
    if(e!=false){
      this.message.success("保存成功")
      this.entity = e;
    }
  }
}
